<?php 

namespace app\wenjian;

class Pay_Money_Api {
    /**
     * 取余额  类型  Cookie  是否模拟人工访问其他页面(达到延长Cookie有效时间的效果)
     */
    function Get_pay_money($Type = NULL ,$Cookie = NULL)
    {
		if($Type=='mq_gzfbyjk'){//云监控(支付宝)
			switch(rand(1,9)){
				case 1:
					$data = $this->Get_Alipay_Cookie('https://personalweb.alipay.com/portal/i.htm',$Cookie);
				break;
				case 2:
					$data = $this->Get_Alipay_Cookie('https://my.alipay.com/wealth/index.html',$Cookie);
 				break;
				case 3:
					$data = $this->Get_Alipay_Cookie('https://110.alipay.com/sc/index.htm',$Cookie);
				break;
				case 4:
					$data = $this->Get_Alipay_Cookie('https://my.alipay.com/portal/i.htm',$Cookie);
				break;
				case 5:
					$data = $this->Get_Alipay_Cookie('https://shanghu.alipay.com/home/switchPersonal.htm',$Cookie);
				break;
				case 6:
					$data = $this->Get_Alipay_Cookie('https://cshall.alipay.com/lab/question.htm',$Cookie);
				break;
				case 7:
					$data = $this->Get_Alipay_Cookie('https://cshall.alipay.com/lab/cateQuestion.htm',$Cookie);
				break;
				case 8:
					$data = $this->Get_Alipay_Cookie('https://cshall.alipay.com/lab/help_detail.htm',$Cookie);
				break;
				case 9:
					$data = $this->Get_Alipay_Cookie('https://egg.alipay.com/egg/index.htm',$Cookie);
				break;
				default:
					$data = $this->Get_Alipay_Cookie('http://egg.alipay.com/egg/advice.htm',$Cookie);
				break;
			}
			$preg = '/{\"totalAvailableBalance\":\"(.*?)\",/is';
			preg_match_all($preg, $data, $trStr);
			error_reporting(0);
			if(!$trStr[1][0]){
				$money  = '获取失败';
				$code = -1;
			}else{
				$money  = $trStr[1][0];
				$code = 1;
			}
		}if($Type=='mq_gqqyjk'){//云监控(Q Q)
			switch(rand(1,9)){
				case 1:
					$data = $this->Get_Qqpay_Cookie('https://www.tenpay.com/v3/account/charge/charge.shtml',$Cookie);
				break;
				case 2:
					$data = $this->Get_Qqpay_Cookie('https://www.tenpay.com/v3/trade/account_detail.shtml',$Cookie);
 				break;
				case 3:
					$data = $this->Get_Qqpay_Cookie('https://www.tenpay.com/v3/account/pay/paycard.shtml',$Cookie);
				break;
				case 4:
					$data = $this->Get_Qqpay_Cookie('https://www.tenpay.com/v3/account/info/index.shtml',$Cookie);
				break;
				case 5:
					$data = $this->Get_Qqpay_Cookie('https://www.tenpay.com/v2/safe/v2/index.shtml',$Cookie);
				break;
				case 6:
					$data = $this->Get_Qqpay_Cookie('https://www.tenpay.com/v2/safe/v2/safe_tool.shtml',$Cookie);
				break;
				case 7:
					$data = $this->Get_Qqpay_Cookie('https://www.tenpay.com/v2/safe/course.shtml',$Cookie);
				break;
				case 8:
					$data = $this->Get_Qqpay_Cookie('https://www.tenpay.com/v2/cs/v2/index.shtml',$Cookie);
				break;
				case 9:
					$data = $this->Get_Qqpay_Cookie('https://www.tenpay.com/v2/safe/course.shtml',$Cookie);
				break;
				default:
					$data = $this->Get_Qqpay_Cookie('https://www.tenpay.com/v3/account/info/index.shtml',$Cookie);
				break;
			}
			$json = json_decode($data, true);
			if($json['retcode']==0 and $json['retmsg']=="OK"){
				$money  = $json['records'][0]['balance'];
				$code = 1;
			}else{
				$money  = '获取失败';
				$code = -1;
			}
		}
		if($code==1){//如果监控不到金额,则COOKIE失效
			$money = str_replace(",","",$money);
			$money = str_replace(".","",$money);
			$money = trim(($money/100));
		}
		return array("code" => $code, "money" => $money, "time" => time(), "cookie" => $Cookie);
    }    

	//支付宝
    protected  function Get_Alipay_Cookie($Url_Referer, $Cookie = null)
	{ 
		$ctoken = $this->getSubstr($Cookie,"ctoken=", ";");
		$Url = 'https://mrchportalweb.alipay.com/user/asset/queryData?_ksTS='.time().'_'.rand(10,99).'&_input_charset=utf-8&ctoken='.$ctoken;
		$referer = $Url_Referer.'?&t='.time();
		$ua = 'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
accept-encoding: gzip, deflate, br
accept-language: zh-CN,zh;q=0.9
cache-control: max-age=0
Cookie: '.@$Cookie.'
referer: '.$Url.'?referer='.$referer.'
upgrade-insecure-requests: 1
user-agent: Mozilla/5.0 (Linux; Android 10.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36 360Browser/9.2.5584.400';
		$result = $this->Get_Money_curl_intl($Url_Referer,0,$referer,$Cookie,0,$ua);
		$result = $this->alipayCurl($Url,$Cookie);
		return $result;
	}
	
	//QQ钱包
    protected  function Get_Qqpay_Cookie($Url_Referer, $Cookie = null)
	{
		$uin = explode("qluin=",$Cookie);
		$res = $this->QQCurl("https://im.qq.com/index",$Cookie);
	    $uin = getSubstr($res, '"uin":', ',"avatar"');
		$skey = $this->getSubstr($Cookie,"skey=", ";");
		$Url = 'https://myun.tenpay.com/cgi-bin/clientv1.0/qwallet_account_list.cgi?limit=1&offset=0&s_time='.date('Y-m-d',time()).'&time_type=0&source_type=7&pay_type=2&ref_param=&skey='.$skey.'&skey_type=2&uin='.$uin;
		$referer = $Url_Referer.'?&t='.time();
		 $ua = 'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
accept-encoding: gzip, deflate, br
accept-language: zh-CN,zh;q=0.9
cache-control: max-age=0
Cookie: '.@$Cookie.'
referer: '.$Url.'?referer='.$referer.'
upgrade-insecure-requests: 1
user-agent: Mozilla/5.0 (Linux; Android 10.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36 QQBrowser/9.2.5584.400';
		$result = $this->Get_Money_curl($Url_Referer,0,$referer,$Cookie,0,$ua);
		$result = $this->QQCurl($Url,$Cookie);
		return $result;
	}
	
	
	protected function alipayCurl($api,$cookie)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $api);
        curl_setopt($ch, CURLOPT_COOKIE, $cookie);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_REFERER, 'https://mrchportalweb.alipay.com/');
        curl_setopt($ch, CURLOPT_USERAGENT,
        "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");
        $res  = curl_exec($ch);
        $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        //$res = mb_convert_encoding($res, 'UTF-8', 'GBK');
        return $res;
   }
	
   //取源代码
    protected  function Get_Money_curl($url,$post=0,$referer=0,$cookie=0,$header=0,$ua=0,$nobaody=0){
		if(is_array($cookie))
		{
			$str = '';
			foreach ($cookie as $key => $value){
				$str .= $key.'='.$value.'; ';
			}
			$cookie = substr($str,0,-1);
		}
    $opts = array(
       'http'=>array(
       'method'=>($post?'POST':'GET'),
       'header'=>"Content-type: application/x-www-form-urlencoded\r\n".
                  "Content-length:".strlen($post)."\r\n" . 
                  "Cookie: ".@$cookie."\r\n" . 
                  "\r\n" . $ua .
                  "\r\n",
       'content' => $post,
       )
    );
    $context = stream_context_create($opts);
    $ret = file_get_contents($url, false, $context);
    return $ret;
	}
   protected  function Get_Money_curl_intl($url,$post=0,$referer=0,$cookie=0,$header=0,$ua=0,$nobaody=0){
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL,$url);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
	$httpheader[] = "Accept:*/*";
	$httpheader[] = "Accept-Encoding:gzip,deflate,sdch";
	$httpheader[] = "Accept-Language:zh-CN,zh;q=0.8";
	$httpheader[] = "Connection:close";
	curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
	curl_setopt($ch, CURLOPT_TIMEOUT, 30);
	if($post){
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
	}
	if($header){
		curl_setopt($ch, CURLOPT_HEADER, TRUE);
	}
	if($cookie){
		curl_setopt($ch, CURLOPT_COOKIE, $cookie);
	}
	if($referer){
		if($referer==1){
			curl_setopt($ch, CURLOPT_REFERER, 'http://m.qzone.com/infocenter?g_f=');
		}else{
			curl_setopt($ch, CURLOPT_REFERER, $referer);
		}
	}
	if($ua){
		curl_setopt($ch, CURLOPT_USERAGENT,$ua);
	}else{
		curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Linux; U; Android 4.4.1; zh-cn; R815T Build/JOP40D) AppleWebKit/533.1 (KHTML, like Gecko)Version/4.0 MQQBrowser/4.5 Mobile Safari/533.1');
	}
	if($nobaody){
		curl_setopt($ch, CURLOPT_NOBODY,1);
	}
	curl_setopt($ch, CURLOPT_ENCODING, "gzip");
	curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
	$ret = curl_exec($ch);
	curl_close($ch);
	return $ret;
	}
	//财付通-模拟人工访问页面    
    protected function QQCurl($api,$cookie)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $api);
        curl_setopt($ch, CURLOPT_COOKIE, $cookie);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_REFERER, 'https://www.tenpay.com/');
        curl_setopt($ch, CURLOPT_USERAGENT,
        "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");
        $res  = curl_exec($ch);
        $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        //$res = mb_convert_encoding($res, 'UTF-8', 'GBK');
        return $res;
   }
   protected  function getSubstr($str, $leftStr, $rightStr) {
        $left = strpos($str, $leftStr);
        //echo '左边:'.$left;
        $right = strpos($str, $rightStr, $left);
        //echo '<br>右边:'.$right;
        if ($left < 0 or $right < $left) return '';
        return substr($str, $left + strlen($leftStr), $right - $left - strlen($leftStr));
    }
    
}
?>